package com.wkbb.sports.controller;

import com.github.pagehelper.PageInfo;
import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.*;
import com.wkbb.common.vo.Tag;
import com.wkbb.sports.dto.SettingDto;
import com.wkbb.sports.feign.BasicFeignService;
import com.wkbb.sports.model.HlifeBanner;
import com.wkbb.sports.model.HlifeSportLevel;
import com.wkbb.sports.service.SettingService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(description = "设置页面接口")
@RequestMapping("/sports/{apiVersion}/setting")
@Slf4j
public class SettingController extends BaseController {

    @Autowired
    private SettingService settingService;
    @Autowired
    private BasicFeignService basicFeignService;

    @ApiOperation(value = "添加 or 修改类型")
    @RequestMapping(value = "putTag", method = RequestMethod.POST)
    public ResultDTO<Boolean> putTag(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @Valid @RequestBody Tag data){
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            data.setUserId(userDto.getId());
            data.setUsername(userDto.getName());
            return settingService.putTag(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "获取标签分页列表")
    @RequestMapping(value = "getTagListByPage", method = RequestMethod.GET)
    public ResultDTO<PageInfo<Tag>> getTagListByPage(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "名称（模糊搜索）", required = false) @RequestParam(name = "name", required = false) String name,
            @ApiParam(value = "标签类型：1课程 2托管 3场地", required = false, defaultValue = "1") @RequestParam(name = "type", required = false) Integer type,
            @ApiParam(value = "页码", required = true, defaultValue = "1") @RequestParam(name = "pageNo", required = true, defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页条数", required = true, defaultValue = "10") @RequestParam(name = "pageSize", required = true, defaultValue = "10") Integer pageSize) {
        UserDto userDto = getUserByLogin();
        if (userDto == null) {
            return new ResultDTO<>(ErrorCode.USER_NOT_EXIST);
        }

        try {
            Map<String, Object> params = new HashMap<>();
            params.put("name", name);
            params.put("type", type);
            return new ResultDTO<>(settingService.getTagListByPage(params, pageNo, pageSize));
        } catch (Exception e) {
            log.error("***********ClassInfoManageController.page******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "获取标签列表")
    @RequestMapping(value = "getTagList", method = RequestMethod.GET)
    public ResultDTO<List<Tag>> getTagList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "标签类型：1课程 2托管 3场地", required = false, defaultValue = "1") @RequestParam(name = "type", required = false) Integer type) {
        UserDto userDto = getUserByLogin();
        if (userDto == null) {
            return new ResultDTO<>(ErrorCode.USER_NOT_EXIST);
        }
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("type", type);
            params.put("enabled", 1);
            return new ResultDTO<>(settingService.getTagList(params));
        } catch (Exception e) {
            log.error("***********ClassInfoManageController.page******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "小程序获取标签列表（兴趣选课、托管选课分类列表）")
    @RequestMapping(value = "getCategoryList", method = RequestMethod.GET)
    public ResultDTO<List<Tag>> getCategoryList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "标签类型：1课程 2托管 3场地", required = false, defaultValue = "1") @RequestParam(name = "type", required = false) Integer type) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("type", type);
            return new ResultDTO<>(settingService.getCategoryList(params));
        } catch (Exception e) {
            log.error("***********ClassInfoManageController.page******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "获取运动关卡列表")
    @RequestMapping(value = "getSportLevelList", method = RequestMethod.GET)
    public ResultDTO<List<HlifeSportLevel>> getSportLevelList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "项目类型 1跳绳", required = false,defaultValue = "1") @RequestParam(name = "projectKind", required = false) Integer projectKind,
            @ApiParam(value = "关卡类型 （1速度 2耐力 3速度耐力）", required = false,defaultValue = "1") @RequestParam(name = "levelType", required = false) Integer levelType){
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        try {
            Map<String,Object>  map  = new HashMap<>();
            map.put("projectKind", projectKind);
            map.put("levelType", levelType);
            return ResultDTO.successResult(settingService.getSportLevelList(map));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "编辑运动关卡")
    @RequestMapping(value = "putSportLevel", method = RequestMethod.POST)
    public ResultDTO<List<HlifeSportLevel>> putSportLevel(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @Valid @RequestBody HlifeSportLevel data){
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            settingService.putSportLevel(data);
            return new ResultDTO<>(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "获取轮播图列表")
    @GetMapping(value = "/getBannerListByPage")
    public ResultDTO getBannerListByPage(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                               @ApiParam(value = "名称", required = false) @RequestParam(name = "name", required = false) String name,
                               @ApiParam(value = "分页数", defaultValue = "1", required = false) @RequestParam(name = "pageNo", defaultValue = "1", required = false) int pageNo,
                               @ApiParam(value = "分页大小", defaultValue = "10", required = false) @RequestParam(name = "pageSize", defaultValue = "10", required = false) int pageSize) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            Map<String, Object> params = new HashMap<>();
            params.put("name",name);
            return new ResultDTO<>(settingService.getBannerListByPage(params,pageNo,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "添加OR编辑轮播图")
    @PostMapping(value = "/putBanner")
    public ResultDTO putBanner(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                                   @Valid @RequestBody HlifeBanner hlifeBanner) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            if (hlifeBanner.getTerminal() == null) hlifeBanner.setTerminal(4);
            settingService.putBanner(hlifeBanner);
            return new ResultDTO<>(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "删除轮播图")
    @PostMapping(value = "/delBanner")
    public ResultDTO delBanner(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                               @ApiParam(value = "编号", required = true) @RequestParam(name = "id", required = true) Long id) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            settingService.delBanner(id);
            return new ResultDTO<>(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "获取学校年级")
    @RequestMapping(value = "getOrgGradeList", method = RequestMethod.GET)
    public ResultDTO<List<GradeDto>> getOrgGradeList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "年级类型(1九年一贯 2十二年一贯 3全部)", required = false, defaultValue = "3") @RequestParam(name = "gradeType", required = false) Integer gradeType)
    {
        try {
            return new ResultDTO(settingService.getOrgGradeList(gradeType));
        } catch (Exception e) {
            log.error("***********getOrgGradeList******", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "获取学校班级列表")
    @RequestMapping(value = "getOrgClassList", method = RequestMethod.GET)
    public ResultDTO<List<ClassInfoDto>> getOrgClassList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "年级ID", required = true) @RequestParam(name = "gradeId", required = true) Long gradeId)
    {
        try {
            return basicFeignService.getOrgClassList(orgId,gradeId,true);
        } catch (Exception e) {
            log.error("***********getOrgGradeList******", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "获取省市区")
    @RequestMapping(value = "getDistrictList", method = RequestMethod.GET)
    public ResultDTO<List<DistrictDto>> getDistrictList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "parentCode(获取省份parentCode填0)", required = true) @RequestParam(name = "parentCode", required = true) Integer parentCode,
            @ApiParam(value = "type(类型 1省份，2城市，3区镇)", required = true) @RequestParam(name = "type", required = true) Integer type)
    {
        return basicFeignService.getDistrictList(parentCode,type);
    }

    @ApiOperation(value = "查询学校设置")
    @RequestMapping(value = "getOrgSetting", method = RequestMethod.GET)
    public ResultDTO<SettingDto> getOrgSetting(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "年级类型(1幼儿园 2小学 3中职 4初中 5高中 6高职 7本科 8完中 9九年一贯 10十二年一贯)", required = false) @RequestParam(name = "gradeType", required = false) Integer gradeType) {

        log.info("orgId:{}", orgId);
        if (null == orgId){
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        try {
            return new ResultDTO(settingService.getOrgSetting(orgId,gradeType));
        } catch (Exception e) {
            log.error("***********setOrgGrade******", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "设置接口")
    @RequestMapping(value = "setOrgSetting", method = RequestMethod.POST)
    public ResultDTO<List<GradeDto>> setOrgSetting(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "设置信息", required = true) @RequestBody SettingDto settingDto) {

        log.info("settingDto:{}", settingDto);
        if (settingDto.getOrgId() == null){
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        UserDto userDto = this.getUserByLogin();
        if(null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }

        try {
            settingDto.setCreateUser(userDto.getName());
            settingDto.setUpdateUser(userDto.getName());
            settingService.saveOrgSetting(settingDto);

            return new ResultDTO(true);
        } catch (Exception e) {
            log.error("***********setOrgGrade******", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }
}
